//
//  XMNavigationController.m
//  XMWeibo
//
//  Created by chi on 14-8-20.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMNavigationController.h"



@interface XMNavigationController ()

@end

@implementation XMNavigationController

+ (void)initialize
{

    // 设置UINavigationBar主题
    [self setupNavigationBarTheme];
    
    // 设置UIBarButtonItem主题
    [self setupUIBarButtonItemTheme];
    
}

/**
 *  设置UINavigationBar主题
 */
+ (void)setupNavigationBarTheme
{
    UINavigationBar *navBar = [UINavigationBar appearance];
    
    // 设置背景
    if (!isIOS7) {
        [navBar setBackgroundImage:[UIImage adaptIOS7ImageWithName:@"navigationbar_background"] forBarMetrics:UIBarMetricsDefault];
    }
    
    // 设置标题字体
    NSMutableDictionary *textAtt = [NSMutableDictionary dictionary];
    textAtt[UITextAttributeTextColor] = [UIColor blackColor];
    textAtt[UITextAttributeFont] = [UIFont systemFontOfSize:17.0];
    textAtt[UITextAttributeTextShadowOffset] = [NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)];

    
    [navBar setTitleTextAttributes:textAtt];

    
}

/**
 *  设置UIBarButtonItem主题
 */
+ (void)setupUIBarButtonItemTheme
{
    UIBarButtonItem *item = [UIBarButtonItem appearance];
    
    // 设置背景图片
    if (!isIOS7) {
        [item setBackgroundImage:[UIImage adaptIOS7ImageWithName:@"navigationbar_button_background"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [item setBackgroundImage:[UIImage adaptIOS7ImageWithName:@"navigationbar_button_background_pushed"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
        [item setBackgroundImage:[UIImage adaptIOS7ImageWithName:@"navigationbar_button_background_disable"] forState:UIControlStateDisabled barMetrics:UIBarMetricsDefault];
    }
    
    
    //设置字体
    
    NSMutableDictionary *normalTextAtt = [NSMutableDictionary dictionary];
    if (!isIOS7) {
        
        normalTextAtt[UITextAttributeTextColor] = XMColor(100, 100, 100);//[UIColor grayColor];
    }
    else {
        normalTextAtt[UITextAttributeTextColor] = [UIColor orangeColor];
    }
    normalTextAtt[UITextAttributeTextShadowOffset] = [NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)];
    
    if (isIOS7) {
        normalTextAtt[UITextAttributeFont] = [UIFont systemFontOfSize:14.0];
    }
    else {
        normalTextAtt[UITextAttributeFont] = [UIFont systemFontOfSize:12.0];
    }
    
    [item setTitleTextAttributes:normalTextAtt forState:UIControlStateNormal];
    [item setTitleTextAttributes:normalTextAtt forState:UIControlStateHighlighted];

    // 设置UIControlStateDisabled
    NSMutableDictionary *disableTextAtt = [normalTextAtt mutableCopy];
    disableTextAtt[UITextAttributeTextColor] = [UIColor lightGrayColor];
    [item setTitleTextAttributes:disableTextAtt forState:UIControlStateDisabled];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage adaptIOS7ImageWithName:@"navigationbar_back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
        viewController.navigationItem.backBarButtonItem.title = nil;
        viewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage adaptIOS7ImageWithName:@"navigationbar_more"] style:UIBarButtonItemStylePlain target:self action:@selector(backToRoot)];
    }
    [super pushViewController:viewController animated:animated];
}

/**
 *  返回上级
 */
- (void)back
{
    [self popViewControllerAnimated:YES];
}

/**
 *  返回顶级
 */
- (void)backToRoot
{
    [self popToRootViewControllerAnimated:YES];
}



@end
